package com.yc.baihe.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.yc.baihe.beans.AdminUser;
import com.yc.baihe.beans.BhUser;
import com.yc.baihe.beans.UserList;
import com.yc.baihe.biz.IAdminUserBiz;
import com.yc.baihe.biz.IBhUserBiz;
import com.yc.baihe.biz.IUserListBiz;
import com.yc.baihe.biz.impl.AdminUserBizImpl;
import com.yc.baihe.biz.impl.BhUserBizImpl;
import com.yc.baihe.biz.impl.UserListBizImpl;
import com.yc.baihe.utils.AgeUtil;
import com.yc.baihe.utils.Reflect;
import com.yc.baihe.utils.SessionAttributeNames;

public class AdminUserServlet extends CommonServlet {
	private static final long serialVersionUID = 1L;
	private PrintWriter out;
	private HttpSession session;
	private IAdminUserBiz adminUserBiz=new AdminUserBizImpl();
	private IBhUserBiz bhUserBiz=new BhUserBizImpl();
	private IUserListBiz userListBiz=new UserListBizImpl();
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);

		out=response.getWriter();
		session=request.getSession();
		
		String op=request.getParameter("op");
		if("getAdminUser".equals(op)){
			getAdminUser(request,response);
		}else if("addAdminUser".equals(op)){//添加管理员信息
			addAdminUser(request,response);
		}else if("delAdminUser".equals(op)){//管理员信息
			delAdminUser(request,response);
		}else if("updateAdminUser".equals(op)){//修改管理员信息
			updateAdminUser(request,response);
		}else if("adminLogin".equals(op)){//管理员登录验证
			adminLogin(request,response);
		}else if("UserPicData".equals(op)){//获取会员头像信息
			UserPicData(request,response);
		}else if("getUserPic".equals(op)){//获取会员头像信息
			getUserPic(request,response);
		}else if("allPicPass".equals(op)){//头像审核通过
			allPicPass(request,response);
		}else if("getTypeData".equals(op)){//获取类型
			getTypeData(request,response);
		}else if("searchPicData".equals(op)){//头像审核的查询按钮
			searchPicData(request,response);
		}else if("getBhUser".equals(op)){//会员基本信息查询
			getBhUser(request,response);
		}else if("delBhUser".equals(op)){//管理员信息
			delBhUser(request,response);
		}else if("addBhUser".equals(op)){//添加百合会员吧
			addBhUser(request,response);
		}
		
		
		
	}
	
	/**
	 * 添加百合会员
	 * @param request
	 * @param response
	 */
	private void addBhUser(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String pwd=request.getParameter("pwd");
		String bhemail=request.getParameter("bhemail");
		String regdate=sdf.format(new Date());
		
		BhUser bu=new BhUser();
		bu.setBhemail(bhemail);
		bu.setBuid(0);
		bu.setPwd(pwd);
		bu.setRegDate(regdate);
		bu.setStatus(1);
		
		out.print(bhUserBiz.addBhUser(bu));
		out.flush();
		out.close();
		
	}

	/**
	 * 删除百合信息
	 * @param request
	 * @param response
	 */
	private void delBhUser(HttpServletRequest request, HttpServletResponse response) {
		String buids=request.getParameter("buids");
		out.print(bhUserBiz.delsBhUser(buids));
		out.flush();
		out.close();
	}

	/**
	 * 得到会员基本信息
	 * @param request
	 * @param response
	 */
	private void getBhUser(HttpServletRequest request, HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		List<BhUser> list=bhUserBiz.find(page,rows);
		JSONArray json=JSONArray.fromObject(list);
		JSONObject jb=new JSONObject();
		jb.put("rows",json);
		jb.put("total", bhUserBiz.getTotal());
		out.print(jb.toString());
		out.flush();
		out.close();
	}

	/**
	 * 头像审核的查询
	 * @param request
	 * @param response
	 */
	private void searchPicData(HttpServletRequest request, HttpServletResponse response) {
		String sex=request.getParameter("sex");
		int age=Integer.parseInt(request.getParameter("age"));
		String looks=request.getParameter("type");	
		
		List<UserList> userslist= userListBiz.findBysal(sex, 0, looks);
		List<UserList> userList=new ArrayList<UserList>();
		
		
		for(UserList u:userslist){
			u.setTemp1(String.valueOf(AgeUtil.getAge(u.getBirthday())));
			if(AgeUtil.getAge(u.getBirthday())==age){
				userList.add(u);
			}
		}
		
		JSONArray json=JSONArray.fromObject(userList);
		JSONObject jb=new JSONObject();
		jb.put("userList",json);
		out.print(jb.toString());
		out.flush();
		out.close();
		
	}

	private void getTypeData(HttpServletRequest request, HttpServletResponse response) {
		String sex=request.getParameter("sex").trim();
		
		List<Map<String,Object>> list=userListBiz.findLooks(sex);
		JSONArray json=JSONArray.fromObject(list);
		JSONObject jb=new JSONObject();
		jb.put("list",json);
		out.print(jb.toString());
		out.flush();
		out.close();
	}

	/**
	 * 后台头像审核
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void allPicPass(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String buids=request.getParameter("buids");
		int status=Integer.parseInt(request.getParameter("status"));//通过为1，不通过为2
		if(userListBiz.updateUserPicStatus(buids,status)>0){
			UserPicData(request,response);//更新成功，重新获取头像相关信息并返回
		}else{
			out.print(userListBiz.updateUserPicStatus(buids,status));//返回0
		}
		//UserPicData(request,response);
		out.flush();
		out.close();
	}
	
	/**
	 * 获取会员头像相关信息
	 * @param request
	 * @param response
	 */
	private void getUserPic(HttpServletRequest request,HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		List<UserList> list=userListBiz.find(page,rows);
		//System.out.println(list);
		JSONArray json=JSONArray.fromObject(list);
		JSONObject jb=new JSONObject();
		jb.put("rows",json);
		jb.put("total", userListBiz.getTotal());
		out.print(jb.toString());
		out.flush();
		out.close();
	}

	/**
	 * 获取会员头像相关信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void UserPicData(HttpServletRequest request,HttpServletResponse response) throws IOException {
		List<UserList> list=userListBiz.find(0, 4);
		for(int i=0;i<list.size();i++){
			String temp1=String.valueOf(AgeUtil.getAge(list.get(i).getBirthday()));
			list.get(i).setTemp1(temp1);
		}
		JSONArray json=JSONArray.fromObject(list);
		JSONObject jb=new JSONObject();
		jb.put("userPicData",json);
		System.out.println(jb.toString());
		out.print(jb.toString());
		out.flush();
		out.close();
		//session.setAttribute(SessionAttributeNames.USERPICDATA, list);
		//response.sendRedirect("back/passPic.jsp");
	}
	
	/**
	 * 管理员登录
	 * @param request
	 * @param response
	 */
	private void adminLogin(HttpServletRequest request,HttpServletResponse response) {
		String aid=request.getParameter("aname");
		String pwd=request.getParameter("pwd");
		
		AdminUser adminUser=adminUserBiz.loginAdmin(aid, pwd);
		session.setAttribute(SessionAttributeNames.CURRENTADMININFO, adminUser);
		if(adminUser!=null){
			out.print(1);
		}else{
			out.print(0);
		}
		out.flush();
		out.close();
	}

	/**
	 * 修改管理员信息
	 * @param request
	 * @param response
	 */
	private void updateAdminUser(HttpServletRequest request,HttpServletResponse response) {
		AdminUser adminInfo=(AdminUser) Reflect.regInfo(request, AdminUser.class);
		
		out.print(adminUserBiz.updateAdmin(adminInfo));
		out.flush();
		out.close();
	}

	/**
	 * 删除管理员
	 * @param request
	 * @param response
	 */
	private void delAdminUser(HttpServletRequest request,HttpServletResponse response) {
		String aids=request.getParameter("aids");
		out.print(adminUserBiz.delAdminInfo(aids));
		out.flush();
		out.close();
	}

	/**
	 * 添加管理员
	 * @param request
	 * @param response
	 */
	private void addAdminUser(HttpServletRequest request,HttpServletResponse response) {
		AdminUser adminInfo=Reflect.regInfo(request,AdminUser.class);
		out.print(adminUserBiz.addAdminInfo(adminInfo));
		out.flush();
		out.close();
	}

	/**
	 * 获取管理员信息
	 * @param request
	 * @param response
	 */
	private void getAdminUser(HttpServletRequest request,HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		List<AdminUser> list=adminUserBiz.find(page,rows);
		JSONArray json=JSONArray.fromObject(list);
		JSONObject jb=new JSONObject();
		jb.put("rows",json);
		jb.put("total", adminUserBiz.getTotal());
		out.print(jb.toString());
		out.flush();
		out.close();
	}

}
